package za.co.dylanjaneke.airdownloader.command
{
	import flash.events.SQLEvent;
	
	import mx.core.FlexGlobals;
	import mx.rpc.events.ResultEvent;
	
	import za.co.dylanjaneke.airdownloader.controller.data.ConnectionManager;
	import za.co.dylanjaneke.airdownloader.events.ShowDownloadQueueEvent;
	import za.co.dylanjaneke.airdownloader.model.DownloadQueuePM;
	import za.co.dylanjaneke.airdownloader.model.ScreenNavigatorPM;
	import za.co.dylanjaneke.airdownloader.model.vo.FileDownloadVO;

	/**
	 * Author: Dylan Janeke.
	 */
	[Name("showDownloadQueueCommand")]
	public class ShowDownloadQueueCommand
	{
		[Inject]
		public var screenNavigator:ScreenNavigatorPM;
		
		[Inject]
		public var repo:ConnectionManager;
		
		[Inject]
		public var downloadQueuePM:DownloadQueuePM;
		
		[Observer]
		public function handleOpenDownloadQueueEvent(event:ShowDownloadQueueEvent): void
		{
			repo.executeStatement(
				FileDownloadVO.createListActive(repo.createStatement()), onLoadFileDownloadResult);
		}
		
		public function onLoadFileDownloadResult(event:SQLEvent): void
		{
			downloadQueuePM.downloads=
				FileDownloadVO.listValue(event);
			
			screenNavigator.switchView(ScreenNavigatorPM.DOWNLOAD_QUEUE);
			VicariousDownloader.resize(389*2);			
		}
	}
}